package com.dhnsoft.shortLink.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import com.dhnsoft.shortLink.admin.common.convention.result.Result;
import com.dhnsoft.shortLink.admin.common.convention.result.Results;
import com.dhnsoft.shortLink.admin.common.enums.UserErrorCodeEnum;
import com.dhnsoft.shortLink.admin.dto.req.UserLoginReqDTO;
import com.dhnsoft.shortLink.admin.dto.req.UserRegisterReqDTO;
import com.dhnsoft.shortLink.admin.dto.req.UserUpateReqDTO;
import com.dhnsoft.shortLink.admin.dto.resp.UserActualRespDTO;
import com.dhnsoft.shortLink.admin.dto.resp.UserLoginRespDTO;
import com.dhnsoft.shortLink.admin.dto.resp.UserRespDTO;
import com.dhnsoft.shortLink.admin.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * ClassName: UserController
 * Package: com.dhnsoft.shortLink.admin.controller
 * Description:
 *
 * @Author dhn
 * @Create 2024/3/14 15:31
 * @Version 1.0
 */
@RestController
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;


    /**
     * 返回脱敏用户信息
     * @param username
     * @return
     */
    @GetMapping("/api/short-link/admin/v1/user/{username}")
    public Result<UserRespDTO> getUserInfo(@PathVariable String username){
        UserRespDTO result = userService.getUserByUsername(username);
        if(result==null){
            return new Result<UserRespDTO>().setCode("B000200").setMessage(UserErrorCodeEnum.USER_NULL.message());
        }
        return Results.success(result);
    }

    /**
     * 返回无脱敏用户信息
     * @param username
     * @return
     */
    @GetMapping("/api/short-link/admin/v1/actual/user/{username}")
    public Result<UserActualRespDTO> getActualUserInfo(@PathVariable String username){
        UserRespDTO result = userService.getUserByUsername(username);
        return Results.success(BeanUtil.toBean(result,UserActualRespDTO.class));
    }

    /**
     * 查询用户名是否存在
     */
    @GetMapping("/api/short-link/admin/v1/user/has-username")
    public Result<Boolean> hasUsername(@RequestParam String username){
        return Results.success(userService.hasUsername(username));
    }

    /**
     * 新增用户
     * */
    @PostMapping("/api/short-link/admin/v1/user")
    public Result<Void> register(@RequestBody UserRegisterReqDTO requestParam){
        userService.register(requestParam);
        return Results.success();
    }

    /**
     * 修改用户
     * */
    @PutMapping("/api/short-link/admin/v1/user")
    public Result<Void> update(@RequestBody UserUpateReqDTO requestParam){
        userService.update(requestParam);
        return Results.success();
    }

    /**
     * 用户登录
     * */
    @GetMapping("/api/short-link/admin/v1/user/login")
    public Result<UserLoginRespDTO> login(@RequestBody UserLoginReqDTO requestParam){
        UserLoginRespDTO result = userService.login(requestParam);
        return Results.success(result);
    }


    /**
     * 检查用户是否登录
     * */
    @GetMapping("/api/short-link/admin/v1/user/checkLogin")
    public Result<Boolean> checkLogin(@RequestParam String username,@RequestParam String token){
        return Results.success(userService.checkLogin(username,token));
    }

    /**
     * 用户退出功能
     * */
    @DeleteMapping("/api/short-link/admin/v1/user/logout")
    public Result<Void> logout(@RequestParam String username,@RequestParam String token){
        userService.logout(username,token);
        return Results.success();
    }

}
